use axum::{async_trait, body::Bytes, extract::{FromRequest, Request}};
use serde::de::DeserializeOwned;

use crate::{error::AppErr, server::Cbor};


#[async_trait]
impl<T: DeserializeOwned, S: Send + Sync> FromRequest<S> for Cbor<T> {

    type Rejection = AppErr;
    
    async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
        let body = Bytes::from_request(req, state).await?;
        let v: T = serde_cbor::from_slice(&body)?;
        Ok(Cbor(v))
    }
}
